package com.stone.cat.adapter;

import com.stone.cat.common.exception.MyBizException;
import com.stone.spring.core.pojo.request.BaseRequestDTO;
import com.stone.spring.core.pojo.response.BaseResponseDTO;
import com.stone.spring.core.service.BaseServiceExecutor;
import com.stone.spring.core.util.InstanceUtil;
import com.stone.spring.core.util.ReflectUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ClassUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * Created by 喵 on 2018/2/1.
 */
@Slf4j
public class MyServiceExecutor<REQ extends BaseRequestDTO, RES extends BaseResponseDTO> extends BaseServiceExecutor<REQ, RES> {

    @Override
    protected RES executeProcess(REQ requestDTO) {

        Class<RES> responseClass = ReflectUtils.getClassGenricType(this.getClass(), 1);

        RES response = InstanceUtil.instance(responseClass);

        try {
            response = this.myProcess(requestDTO);
        } catch (MyBizException e) {
            response.setCode(BaseResponseDTO.CODE_FAIL).setMessage(e.getMessage());
            log.info("业务处理异常,{}",e.getMessage());
        } catch (DuplicateKeyException e) {
            response.setCode(BaseResponseDTO.CODE_FAIL).setMessage("数据重复");
            log.info("数据重复异常,{}",e.getMessage());
        } catch (Exception e) {
            response.setCode(BaseResponseDTO.CODE_FAIL).setMessage("其他异常");
            log.info("其他异常,{}",e.getMessage());
        }
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        return response;
    }

    protected RES myProcess(REQ requestDTO) {
        return null;
    }
}
